package com.peiandsky;

import java.util.HashMap;
import java.util.Random;

import android.annotation.SuppressLint;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.provider.Settings.System;

public class PlaySound {
	private static MediaPlayer music;
	private static SoundPool soundPool;

	private static boolean musicSt = true; // 音乐开关
	private static boolean soundSt = true; // 音效开关
	private static Context context;
	private static int currentPlayingMusic = 0;
	private static Random rand =new Random();
	int[][] numseven = new int[][] { { 10, 20, 30 }, { 40, 50 }, { 60 } };
	private static final int[][] buyaoSound = new int[][] {
			{ R.raw.buyao1_man, R.raw.buyao3_man, R.raw.buyao3_man,
					R.raw.buyao4_man },
			{ R.raw.buyao1_woman, R.raw.buyao2_woman, R.raw.buyao3_woman,
					R.raw.buyao4_woman } };
	private static final int[][] daniSound = new int[][] {
			{ R.raw.dani1_man, R.raw.dani2_man, R.raw.dani3_man },
			{ R.raw.dani1_woman, R.raw.dani2_woman, R.raw.dani3_woman } };
	private static final int[][] duiSound = new int[][] {
			{ R.raw.dui3_man, R.raw.dui4_man, R.raw.dui5_man, R.raw.dui6_man,
					R.raw.dui7_man, R.raw.dui8_man, R.raw.dui9_man,
					R.raw.dui10_man, R.raw.dui11_man, R.raw.dui12_man,
					R.raw.dui13_man, R.raw.dui1_man, R.raw.dui2_man },
			{ R.raw.dui3_woman, R.raw.dui4_woman, R.raw.dui5_woman,
					R.raw.dui6_woman, R.raw.dui7_woman, R.raw.dui8_woman,
					R.raw.dui9_woman, R.raw.dui10_woman, R.raw.dui11_woman,
					R.raw.dui12_woman, R.raw.dui13_woman, R.raw.dui1_woman,
					R.raw.dui2_woman, } };

	private static final int[][] pokeSound = new int[][] {
			{ R.raw.poke3_man, R.raw.poke4_man, R.raw.poke5_man,
					R.raw.poke6_man, R.raw.poke7_man, R.raw.poke8_man,
					R.raw.poke9_man, R.raw.poke10_man, R.raw.poke11_man,
					R.raw.poke12_man, R.raw.poke13_man, R.raw.poke1_man,
					R.raw.poke2_man, R.raw.poke14_man, R.raw.poke15_man, },
			{ R.raw.poke3_woman, R.raw.poke4_woman, R.raw.poke5_woman,
					R.raw.poke6_woman, R.raw.poke7_woman, R.raw.poke8_woman,
					R.raw.poke9_woman, R.raw.poke10_woman, R.raw.poke11_woman,
					R.raw.poke12_woman, R.raw.poke13_woman, R.raw.poke1_woman,
					R.raw.poke2_woman, R.raw.poke14_woman, R.raw.poke15_woman } };

	private static final int[][] baojingSound = new int[][] {
			{ R.raw.baojing1_man, R.raw.baojing2_man },
			{ R.raw.baojing1_woman, R.raw.baojing2_woman } };
	private static final int[][] buqiangSound = new int[][] {
			{ R.raw.buqiang1_man, R.raw.buqiang2_man },
			{ R.raw.buqiang1_woman, R.raw.buqiang2_woman } };
	private static final int[][] qiangdizhuSound = new int[][] {
			{ R.raw.qiangdizhu1_man, R.raw.qiangdizhu2_man,
					R.raw.qiangdizhu3_man },
			{ R.raw.qiangdizhu1_woman, R.raw.qiangdizhu2_woman,
					R.raw.qiangdizhu3_woman } };

	private static final int MALE = 0;
	private static final int FEMALE = 1;

	private static int buyaoSoundMap[][] = new int[2][4]; // 音效资源id与加载过后的音源id的映射关系表
	private static int daniSoundMap[][] = new int[2][3];;
	private static int duiSoundMap[][] = new int[2][13];;
	private static int pokeSoundMap[][] = new int[2][15];;

	private static int baojingSoundMap[][] = new int[2][2];;
	private static int buqiangSoundMap[][] = new int[2][2];;
	private static int qiangdizhuSoundMap[][] = new int[2][3];;

	private static int feijiSoundMap[] = new int[2];
	private static int lianduiSoundMap[] = new int[2];
	private static int sandaiyiSoundMap[] = new int[2];
	private static int sandaiyiduiSoundMap[] = new int[2];
	private static int sangeSoundMap[] = new int[2];
	private static int shunziSoundMap[] = new int[2];
	private static int sidaierSoundMap[] = new int[2];
	private static int sidailiangduiSoundMap[] = new int[2];
	private static int wangzhaSoundMap[] = new int[2];
	private static int zhadanSoundMap[] = new int[2];
	private static int jiaodizhuSoundMap[] = new int[2];
	private static HashMap<Integer, Integer> otherSoundMap;

	/*
	public PlaySound(Context context) { init(context); }
	 */
	/**
	 * 初始化方法
	 * 
	 * @param c
	 */
	public static void init(Context c) {
		context = c;
		initSound();
	}

	// 初始化音效播放器
	@SuppressLint("UseSparseArrays") private static void initSound() {
		soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 100);
		otherSoundMap = new HashMap<Integer, Integer>();

		for (int i = 0; i < 4; i++) {
			buyaoSoundMap[MALE][i] = soundPool.load(context,
					buyaoSound[MALE][i], 1);
			buyaoSoundMap[FEMALE][i] = soundPool.load(context,
					buyaoSound[FEMALE][i], 1);
		}
		for (int i = 0; i < 3; i++) {
			daniSoundMap[MALE][i] = soundPool.load(context, daniSound[MALE][i],
					1);
			daniSoundMap[FEMALE][i] = soundPool.load(context,
					daniSound[FEMALE][i], 1);
		}
		for (int i = 0; i < 13; i++) {
			duiSoundMap[MALE][i] = soundPool
					.load(context, duiSound[MALE][i], 1);
			duiSoundMap[FEMALE][i] = soundPool.load(context,
					duiSound[FEMALE][i], 1);
		}
		for (int i = 0; i < 15; i++) {
			pokeSoundMap[MALE][i] = soundPool.load(context, pokeSound[MALE][i],
					1);
			pokeSoundMap[FEMALE][i] = soundPool.load(context,
					pokeSound[FEMALE][i], 1);
		}

		for (int i = 0; i < 2; i++) {
			baojingSoundMap[MALE][i] = soundPool.load(context,
					baojingSound[MALE][i], 1);
			baojingSoundMap[FEMALE][i] = soundPool.load(context,
					baojingSound[FEMALE][i], 1);
		}
		for (int i = 0; i < 2; i++) {
			buqiangSoundMap[MALE][i] = soundPool.load(context,
					buqiangSound[MALE][i], 1);
			buqiangSoundMap[FEMALE][i] = soundPool.load(context,
					buqiangSound[FEMALE][i], 1);
		}
		for (int i = 0; i < 3; i++) {
			qiangdizhuSoundMap[MALE][i] = soundPool.load(context,
					qiangdizhuSound[MALE][i], 1);
			qiangdizhuSoundMap[FEMALE][i] = soundPool.load(context,
					qiangdizhuSound[FEMALE][i], 1);
		}

		feijiSoundMap[MALE] = soundPool.load(context, R.raw.feiji_man, 1);
		feijiSoundMap[FEMALE] = soundPool.load(context, R.raw.feiji_woman, 1);
		lianduiSoundMap[MALE] = soundPool.load(context, R.raw.liandui_man, 1);
		lianduiSoundMap[FEMALE] = soundPool.load(context, R.raw.liandui_woman,
				1);
		sandaiyiSoundMap[MALE] = soundPool.load(context, R.raw.sandaiyi_man, 1);
		sandaiyiSoundMap[FEMALE] = soundPool.load(context,
				R.raw.sandaiyi_woman, 1);
		sandaiyiduiSoundMap[MALE] = soundPool.load(context,
				R.raw.sandaiyidui_man, 1);
		sandaiyiduiSoundMap[FEMALE] = soundPool.load(context,
				R.raw.sandaiyidui_woman, 1);
		sangeSoundMap[MALE] = soundPool.load(context, R.raw.sange_man, 1);
		sangeSoundMap[FEMALE] = soundPool.load(context, R.raw.sange_woman, 1);
		shunziSoundMap[MALE] = soundPool.load(context, R.raw.shunzi_man, 1);
		shunziSoundMap[FEMALE] = soundPool.load(context, R.raw.shunzi_woman, 1);
		sidaierSoundMap[MALE] = soundPool.load(context, R.raw.sidaier_man, 1);
		sidaierSoundMap[FEMALE] = soundPool.load(context, R.raw.sidaier_woman,
				1);
		wangzhaSoundMap[MALE] = soundPool.load(context, R.raw.wangzha_man, 1);
		wangzhaSoundMap[FEMALE] = soundPool.load(context, R.raw.wangzha_woman,
				1);
		sidailiangduiSoundMap[MALE] = soundPool.load(context,
				R.raw.sidailiangdui_man, 1);
		sidailiangduiSoundMap[FEMALE] = soundPool.load(context,
				R.raw.sidailiangdui_woman, 1);
		zhadanSoundMap[MALE] = soundPool.load(context, R.raw.zhadan_man, 1);
		zhadanSoundMap[FEMALE] = soundPool.load(context, R.raw.zhadan_woman, 1);
		
		jiaodizhuSoundMap[MALE] = soundPool.load(context, R.raw.jiaodizhu_man, 1);
		jiaodizhuSoundMap[FEMALE] = soundPool.load(context, R.raw.jiaodizhu_woman, 1);
		

		otherSoundMap.put(R.raw.plane, soundPool.load(context, R.raw.plane, 1));
		otherSoundMap.put(R.raw.boom, soundPool.load(context, R.raw.boom, 1));
		otherSoundMap.put(R.raw.alert, soundPool.load(context, R.raw.alert, 1));
		otherSoundMap.put(R.raw.start, soundPool.load(context, R.raw.start, 1));
		otherSoundMap.put(R.raw.flower,
				soundPool.load(context, R.raw.flower, 1));
		otherSoundMap.put(R.raw.givecard,
				soundPool.load(context, R.raw.givecard, 1));
	}

	// 初始化音乐播放器
	private static void initAndPlayMusic(int musicId, boolean loop) {
		// int r = new Random().nextInt(musicId.length);
		currentPlayingMusic = musicId;
		music = MediaPlayer.create(context, musicId);
		music.setLooping(loop);
		music.start();
	}

	/**
	 * 播放音效
	 * 
	 * @param resId
	 *            音效资源id
	 * 
	 *            public static void playSound(int resId) { //Integer soundId =
	 *            soundMap.get(resId); if(soundId != null) soundPool.play(resId,
	 *            1, 1, 1, 0, 1); }
	 */

	/**
	 * 暂停音乐
	 * 
	 * public static void pauseMusic() { if(music.isPlaying()) music.pause(); }
	 */

	/**
	 * 播放音乐
	 * 
	 * public static void startMusic() { if(musicSt) music.start(); }
	 */

	/**
	 * 切换一首音乐并播放
	 * 
	 * public static void changeAndPlayMusic() { if(music != null)
	 * music.release(); initMusic(); startMusic(); }
	 */
	/**
	 * 获得音乐开关状态
	 * 
	 * @return
	 * 
	 *         public static boolean isMusicSt() { return musicSt; }
	 */

	/**
	 * 设置音乐开关
	 * 
	 * @param musicSt
	 * 
	 *            public static void setMusicSt(boolean musicSt) {
	 *            SoundPlayer.musicSt = musicSt; if(musicSt) music.start(); else
	 *            music.stop(); }
	 */
	/**
	 * 获得音效开关状态
	 * 
	 * @return
	 * 
	 *         public static boolean isSoundSt() { return soundSt; }
	 */
	/**
	 * 设置音效开关
	 * 
	 * @param soundSt
	 * 
	 *            public static void setSoundSt(boolean soundSt) {
	 *            SoundPlayer.soundSt = soundSt; }
	 */

	/**
	 * 播放背景音乐
	 */
	public static void playBackgroundMusic() {
		if (musicSt == false)
			return;

		if (music != null)
			music.release();
		initAndPlayMusic(R.raw.normal, true);
	}

	public static void stopMusic() {
		if (music != null) {
			if (music.isPlaying()) {
				music.stop();
			}
		}
	}

	public static void playWinMusic() {
		if (musicSt == false)
			return;

		if (music != null)
			music.release();
		initAndPlayMusic(R.raw.win, false);
	}

	public static void playLoseMusic() {
		if (musicSt == false)
			return;
		if (music != null)
			music.release();
		initAndPlayMusic(R.raw.lost, false);
	}

	public static void playExcitingMusic() {
		if (musicSt == false)
			return;
		if( currentPlayingMusic == R.raw.exciting )
			return;
		
		if (music != null)
			music.release();
		initAndPlayMusic(R.raw.exciting, true);
	}

	// firstShow:是否是本轮第一个出牌者
	public static void playPokeTypeSound(int sex, int pokeType, int pokeValue,
			boolean firstShow) {

		Integer soundRes;
		Integer soundId;

		switch (pokeType) {
		case PokeType.danpai:
			soundRes = pokeSoundMap[sex][pokeValue - 3];
			if (soundRes != null)
				soundPool.play(soundRes, 1, 1, 1, 0, 1);
			break;

		case PokeType.duipai:
			soundRes = duiSoundMap[sex][pokeValue - 3];
			if (soundRes != null)
				soundPool.play(soundRes, 1, 1, 1, 0, 1);
			break;

		case PokeType.danshun:
			if (firstShow) {
				soundRes = shunziSoundMap[sex];
				if (soundRes != null)
					soundPool.play(soundRes, 1, 1, 1, 0, 1);
			} else {
				playDaniSound(sex);
			}

			break;
		case PokeType.feiji:
			if (firstShow) {
				soundRes = feijiSoundMap[sex];
				if (soundRes != null)
					soundPool.play(soundRes, 1, 1, 1, 0, 1);
			} else {
				playDaniSound(sex);
			}
			soundPool.play(otherSoundMap.get(R.raw.plane), 1, 1, 1, 0, 1);

			break;
		case PokeType.huojian:
				soundRes = wangzhaSoundMap[sex];
				if (soundRes != null)
					soundPool.play(soundRes, 1, 1, 1, 0, 1);
				soundPool.play(otherSoundMap.get(R.raw.boom), 1, 1, 1, 0, 1);
				playExcitingMusic();
			break;
		case PokeType.sandaiyi:
			if (firstShow) {
				soundRes = sandaiyiSoundMap[sex];
				if (soundRes != null)
					soundPool.play(soundRes, 1, 1, 1, 0, 1);
			} else {
				playDaniSound(sex);
			}

			break;
		case PokeType.sanshun:
			if (firstShow) {
				soundRes = shunziSoundMap[sex];
				if (soundRes != null)
					soundPool.play(soundRes, 1, 1, 1, 0, 1);
			} else {
				playDaniSound(sex);
			}

			break;
		case PokeType.sanzhang:
			if (firstShow) {
				soundRes = sangeSoundMap[sex];
				if (soundRes != null)
					soundPool.play(soundRes, 1, 1, 1, 0, 1);
			} else {
				playDaniSound(sex);
			}

			break;
		case PokeType.shuangshun:
			if (firstShow) {
				soundRes = lianduiSoundMap[sex];
				if (soundRes != null)
					soundPool.play(soundRes, 1, 1, 1, 0, 1);
			} else {
				playDaniSound(sex);
			}

			break;
		case PokeType.zhadan:
			soundRes = zhadanSoundMap[sex];
			if (soundRes != null)
				soundPool.play(soundRes, 1, 1, 1, 0, 1);
			soundPool.play(otherSoundMap.get(R.raw.boom), 1, 1, 1, 0, 1);
			playExcitingMusic();
			break;
		case PokeType.sidaier:
			if (firstShow) {
				soundRes = sidaierSoundMap[sex];
				if (soundRes != null)
					soundPool.play(soundRes, 1, 1, 1, 0, 1);
			} else {
				playDaniSound(sex);
			}

			break;
		default:
			break;
		}

	}

	public static void playBuyaoSound(int sex) {
		if (soundSt == false)
			return;

		Integer soundId = rand.nextInt(buyaoSoundMap[sex].length);
		Integer soundRes = buyaoSoundMap[sex][soundId];

		if (soundRes != null)
			soundPool.play(soundRes, 1, 1, 1, 0, 1);
	}
	
	public static void playStartSound() {
		if (soundSt == false)
			return;
		
		Integer soundRes = otherSoundMap.get(R.raw.start);

		if (soundRes != null)
			soundPool.play(soundRes, 1, 1, 1, 0, 1);
	}

	public static void playDaniSound( int sex ) {
		if (soundSt == false)
			return;

		Integer soundId = rand.nextInt(daniSoundMap[sex].length);
		Integer soundRes = daniSoundMap[sex][soundId];

		if (soundRes != null)
			soundPool.play(soundRes, 1, 1, 1, 0, 1);
	}

	public static void playQiangDiZhuSound( int sex ) {
		if (soundSt == false)
			return;

		Integer soundId = rand.nextInt(qiangdizhuSoundMap[sex].length);
		Integer soundRes = qiangdizhuSoundMap[sex][soundId];

		if (soundRes != null)
			soundPool.play(soundRes, 1, 1, 1, 0, 1);
	}
	
	public static void playBuQiangSound( int sex ) {
		if (soundSt == false)
			return;

		Integer soundId = rand.nextInt(buqiangSoundMap[sex].length);
		Integer soundRes = buqiangSoundMap[sex][soundId];

		if (soundRes != null)
			soundPool.play(soundRes, 1, 1, 1, 0, 1);
	}
	
	public static void playJiaoDiZhuSound( int sex ) {
		if (soundSt == false)
			return;
		
		Integer soundRes = jiaodizhuSoundMap[sex];

		if (soundRes != null)
			soundPool.play(soundRes, 1, 1, 1, 0, 1);
	}

}